﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using System.Resources;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class RegisterAdministrator : TemplateForm
    {
        private ResourceManager resours;
        
        private bool isUpdate = false;
        private Administrator admi = new Administrator();
        private string userNameUpdate;
        private string passwordUpdate;
        public RegisterAdministrator()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
        }
        public RegisterAdministrator(string userName, string password)
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
            this.userNameUpdate = userName;
            this.passwordUpdate = password;
            admi = AdministratorManager.GetAdministratorByUserName(userName, password);
            this.txtUserName.Text = admi.UserName;
            this.txtPassword.Text = admi.Password;
            this.txtRepeadPassword.Text = admi.Password;
            this.txtName.Text = admi.Name;
            this.txtSurname.Text = admi.Surname;
            this.txtEmail.Text = admi.Email;
            this.isUpdate = true;
        }
        private void bRegister_Click(object sender, EventArgs e)
        {
            if (!string.Equals(txtPassword.Text.Trim(),txtRepeadPassword.Text.Trim()))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "The password repeat incorrect", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                try
                {
                    if (isUpdate)
                    {
                        AdministratorManager.ValidateForUpdateAdministrator(userNameUpdate,passwordUpdate,txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtName.Text.Trim(), txtSurname.Text.Trim(), txtEmail.Text.Trim());
                    }
                    else
                    {
                        AdministratorManager.ValidateAdministrator(txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtName.Text.Trim(), txtSurname.Text.Trim(), txtEmail.Text.Trim());
                    }
                    this.Close();
                }
                catch (ArgumentException ex)
                {
                    string message = ex.Message;
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }

            }
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
